package com.itheima.article.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.article.pojo.ApArticleConfig;
import com.itheima.article.service.ApArticleConfigService;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.wemedia.dto.DownOrUpDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.article.listener
 */
@Component
@Slf4j
public class ApArticleConfigListener {

    @Resource
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_DOWN_OR_UP_TOPIC)
    public void consumerMsg(ConsumerRecord<String,String> msg){
        int p = msg.partition();
        long o = msg.offset();
        String json = msg.value();
        log.info("文章上下架同步操作: {},{},{}", p,o,json);
        // 把json消息转成java对象
        DownOrUpDto downOrUpDto = JSON.parseObject(json, DownOrUpDto.class);
        // 文章更新上下架
        apArticleConfigService.downOrUp(downOrUpDto);
        log.info("文章上下架同步操作完成: {},{},{}", p,o,json);
    }
}
